package cn.kgc.controller;


import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppInfo;
import cn.kgc.pojo.BackendUser;
import cn.kgc.service.BackendUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/manager1")
public class MangerController {

    @Resource
    private BackendUserService backendUserService;


    /**
     * 登录界面
     * @param userCode
     * @param userPassword
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/dologin")
    public String loginUser(@RequestParam String userCode,
                            @RequestParam String userPassword,
                            HttpServletRequest request,
                            HttpSession session){
        //判断账户密码是否为空
        if (userCode==null||"".equals(userCode)||userPassword==null||"".equals(userPassword)){
            request.setAttribute("error","账号或密码不能为空");
            return "backendlogin";
        }
        //查找输入的账户密码
        BackendUser backendUser =backendUserService.findByUser(userCode,userPassword);
            if (backendUser!=null){
                //存入session中账户名与密码
                session.setAttribute("userSession",backendUser);
                return "backend/main";
            }else {
                //如果不存在，则返回登录界面
                request.setAttribute("error","账户密码错误");
                return "backendlogin";
            }
    }

    /**
     * 二级菜单，已完成
     * @param pid
     * @return
     */
    @RequestMapping("/categorylevellist")
    @ResponseBody
    public List<AppCategory> two(@RequestParam Integer pid){
        List<AppCategory> data=new ArrayList<>();
        Integer queryCategoryLevel1=pid;
        if (queryCategoryLevel1!=null || !"".equals(queryCategoryLevel1)){
            data=backendUserService.categoryLevelList(queryCategoryLevel1);
        }
        return data;
    }

    /**
     * 三级菜单，已完成
     * @param pid
     * @return
     */
    @RequestMapping("/categorylevellist2")
    @ResponseBody
    public List<AppCategory> there(@RequestParam Integer pid){
        List<AppCategory> data=new ArrayList<>();
        Integer queryCategoryLevel2=pid;
        if (queryCategoryLevel2!=null||!"".equals(queryCategoryLevel2)){
            data=backendUserService.categoryLevelList2(queryCategoryLevel2);
        }
        return data;
    }



    /**
     * 分页+查询，预计完成
     * @param querySoftwareName 软件名称
     * @param queryFlatformId 所属平台
     * @param pageIndex 当前页
     * @param session 账户名与密码
     * @param queryCategoryLevel1 一级查询
     * @param queryCategoryLevel2 二级查询
     * @param queryCategoryLevel3 三级查询
     * @return 查询所收到数据的集合
     */
    @RequestMapping("/applist")
    public String findBackendUser(@RequestParam(required = false) String querySoftwareName,
                                                   @RequestParam(required = false)Integer queryFlatformId,
                                                   @RequestParam(required = false) Integer pageIndex,
                                                   HttpSession session,
                                                   @RequestParam(required = false) Integer queryCategoryLevel1,
                                                   @RequestParam(required = false) Integer queryCategoryLevel2,
                                                   @RequestParam(required = false) Integer queryCategoryLevel3,
                                                   ModelMap modelMap,
                                  HttpServletRequest request) {

        //用作封装
        HashMap<String,Object> rtr=new HashMap<>();
        //传入数据，在impl层中，实现分页与多级查询的方法
        HashMap<String,Object> rt=backendUserService.findBackendUser(querySoftwareName,queryFlatformId,pageIndex,
                queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3);

        List<AppCategory> two = this.two(queryCategoryLevel1);
        List<AppCategory> there = this.there(queryCategoryLevel2);
        BackendUser backendUser = (BackendUser) session.getAttribute("userSession");
        //分页封装
        HashMap<String,Object> pages=new HashMap<>();
        pages.put("totalPageCount",rt.get("totalPageCount"));
        pages.put("totalCount",rt.get("totalCount"));
        pages.put("currentPageNo",rt.get("currentPageNo"));
        //将得到的数据，用modelmap封装
        modelMap.addAttribute("flatFormList",rt.get("type"));
        modelMap.addAttribute("categoryLevel1List",rt.get("queryCategoryLevel1"));
        modelMap.addAttribute("categoryLevel2List",two);
        modelMap.addAttribute("categoryLevel3List",there);
        modelMap.addAttribute("appInfoList",rt.get("findSome"));
        modelMap.addAttribute("pages",pages);
        modelMap.addAttribute("userSession",backendUser);
        //return applist；
        return "backend/applist";
    }


    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "index.jsp";
    }
}
